package com.example.myapplication.kotlin

class Car {
    fun start() {
        println("start")
    }

    fun run() {
        println("run")
    }

    fun stop() {
        println("stop")
    }
}

fun main() {
    // let，安全处理，类似于空安全
//    val name: String? = null
    val name: String? = "悟空"
    name?.let {
        println("name: $it")  // 不为空时输出
    }

    // with 对象上下文操作
    val car = Car()
    with(car) {
        start()
        run()
        stop()
    }

    // run 作用域内计算
    var num1: Int = 100
    var num2 = run {
        var num1 = 10
        num1++
        num1--
        println(num1)
        num1
    }
    println(num1)


    // apply 作用域内对象
    val car2 = Car().apply {
        start()
        run()
        stop()
    }
}